/**
 */
package com.tjian.fafa.user.module.settings.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.TextView;

import com.baidu.mapapi.radar.RadarSearchManager;
import com.tjian.app.AppContextFafa;
import com.tjian.fafa.user.R;
import com.tjian.fafa.user.base.ConnectedApp;
import com.tjian.fafa.user.base.bean.UserInfo;
import com.tjian.fafa.user.config.ConfigOfFafa;
import com.tjian.fafa.user.util.login.LoginActivityUtil;

import net.oschina.app.widget.togglebutton.ToggleButton;

/**
 */
public class SettingsActivity extends Activity
{
    private TextView logoutTextView;

    ToggleButton toggleButton;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.settings_ankuai);

        initView();//实例化视图
        initData();//实例化数据
    }

    public void initView()
    {
        //实例化按钮
        logoutTextView = (TextView) findViewById(R.id.LogoutRelativeLayoutId);
        toggleButton = (ToggleButton) findViewById(R.id.stopUploadLocationId);

        Boolean stopUploadLocation_keyBoolean = AppContextFafa.get(ConfigOfFafa.stopUploadLocation_key, true);

        if (stopUploadLocation_keyBoolean)
        {
            toggleButton.setToggleOn();
        }
        else
        {
            toggleButton.setToggleOff();
        }

        UserInfo me = ConnectedApp.getInstance().getUserInfo();
        //验证是否登录。
        if (me != null)
        {
            logoutTextView.setVisibility(View.VISIBLE);
        }
        logoutTextView.setOnClickListener(new OnClickListener()
        {
            public void onClick(View v)
            {
                AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(SettingsActivity.this);
                alertDialogBuilder.setTitle(null);
                alertDialogBuilder
                        .setMessage("确定要注销本次登录吗?")
                        .setCancelable(false)
                        .setPositiveButton("是", new DialogInterface.OnClickListener()
                        {
                            public void onClick(DialogInterface dialog, int id)
                            {
                                LoginActivityUtil.logout();
                            }
                        }).setNegativeButton("取消", new DialogInterface.OnClickListener()
                {
                    public void onClick(DialogInterface dialog, int id)
                    {
                        dialog.cancel();
                    }
                });
                AlertDialog alertDialog = alertDialogBuilder.create();
                alertDialog.show();
            }
        });
    }

    public void initData()
    {
        //开关更改事件
        toggleButton.setOnToggleChanged(new ToggleButton.OnToggleChanged()
        {
            @Override
            public void onToggle(boolean on)
            {
                AppContextFafa.setLoadImage(on);

                if (!on)
                {
                    stopUploadClick(on);
                }
            }
        });
    }

    /**
     * 停止自动上传
     */
    public void stopUploadClick(boolean on)
    {
        //        ConfigOfFafa.uploadAuto = false;
        RadarSearchManager.getInstance().stopUploadAuto();
    }

    @Override
    protected void onDestroy()
    {
        super.onDestroy();
    }

}